package com.examination.dao;

import com.examination.bean.ExamePaper;
import com.examination.bean.ExamePaperDetail;
import com.examination.bean.ExamePaperScore;
import com.examination.bean.Selection;
import com.examination.utils.DbUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.*;
import java.util.List;
import java.util.Map;

/**
 * @Auther: GongXl
 * @Date: 027 16:39
 * @Description:
 */
public class ExamePaperDetailDao {

    public List<Map> getExamePaperDetailList(ExamePaperDetail examePaperDetail){
        List<Map> examePaperDetailList = null;
//        List<Map<String,Object>> objMapList = null;
        //数据库查询得到的
        Connection conn = DbUtil.getConnInstance();
        //3.操作数据库，实现增删改查
        ResultSet rs = null;
        PreparedStatement stmt = null;
        try {
            String sql = "SELECT e_p_d.id e_p_d_id,e_p_d.exame_paper_id,e_p_d.scheme_type,e_p_d.quest_id,e_s.* from e_exame_paper_detail e_p_d " +
                    "INNER JOIN e_selection e_s " +
                    "on e_p_d.quest_id = e_s.id " +
                    "where scheme_type = ? and exame_paper_id = ? order by e_p_d.quest_id asc ";
            stmt = conn.prepareStatement(sql);
            stmt.setString(1,examePaperDetail.getSchemeType());
            stmt.setLong(2,examePaperDetail.getExamePaperId());
            rs = stmt.executeQuery();
            try {
                examePaperDetailList = DbUtil.getObjectMapList(rs);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return examePaperDetailList;
    }

    /**
     * 返回试卷列表
     * @param examePaper
     * @return
     */
    public List<ExamePaper> getExamePaperList(ExamePaper examePaper) {
        List<ExamePaper> examePaperlList = null;
//        List<Map<String,Object>> objMapList = null;
        //数据库查询得到的
        Connection conn = DbUtil.getConnInstance();
        //3.操作数据库，实现增删改查
        ResultSet rs = null;
        PreparedStatement stmt = null;
        try {
            String sql = "select * from e_exame_paper";
            stmt = conn.prepareStatement(sql);
//            stmt.setLong(2,examePaperDetail.getExamePaperId());
            rs = stmt.executeQuery();
            try {
                examePaperlList = DbUtil.getObjectList(ExamePaper.class,rs);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return examePaperlList;
    }

    /**
     * 添加学生考试记录
     * @param examePaperScore
     * @return
     */
    public String addExamePaperScore(ExamePaperScore examePaperScore){
        Connection conn = DbUtil.getConnInstance();
        //3.操作数据库，实现增删改查
        long row = -1;
        PreparedStatement stmt = null;
        try {
            String sql = "INSERT into e_stu_exame_paper_score (stu_id,exame_paper_id,create_time) values(?,?,SYSDATE())";
            stmt = conn.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);//支持返回主键，auto_increment
            stmt.setLong(1,examePaperScore.getStuId());
            stmt.setLong(2,examePaperScore.getExamePaperId());
            row = stmt.executeUpdate();
            ResultSet generatedKeys = stmt.getGeneratedKeys();
            while (generatedKeys.next()) {
                row = generatedKeys.getLong(1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return row+"";
    }
    /**
     * @Author GongXl
     * @Description 查询同学考试的所有题目信息
     * @Date 029 16:17
     * @Param [examePaperScore]
     * @return java.util.List<java.util.Map>
     **/
    public List<Map> getExamePaperQuestMapList(ExamePaperScore examePaperScore){
        List<Map> questMapList = null;
        Connection conn = DbUtil.getConnInstance();
        ResultSet rs = null;
        PreparedStatement stmt = null;
        try {
            String sql = "select eps.id eps_id, eps.stu_id,eps.exame_paper_id" +
                    ",epd.quest_id,epd.scheme_type" +
                    " from e_stu_exame_paper_score eps" +
                    " INNER JOIN e_exame_paper_detail epd" +
                    " on eps.exame_paper_id = epd.exame_paper_id" +
                    " where eps.id = ?";
            stmt = conn.prepareStatement(sql);
            stmt.setLong(1,examePaperScore.getId());
            rs = stmt.executeQuery();

            try {
                questMapList = DbUtil.getObjectMapList(rs);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return questMapList;
    }
}